package com.gupao.study.decorator;

import lombok.Data;

import java.lang.reflect.Constructor;

@Data
public class User{
    private Navigation navigation;
    public User(){
        //默认权限就是游客
        this.navigation = new Tourist();
    }

    /**
     * 增加相应的权限
     * @param clazz
     */
    public void addAuthority(Class clazz) {
        try {
            //通过反射，获取构造器
            Constructor constructor=clazz.getConstructor(Navigation.class);
            //根据获取的构造函数和参数，创建实例
            DecoratorNavigation navigation=(DecoratorNavigation)constructor.newInstance(this.navigation);
            this.navigation = navigation;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void printNavigation(){
        System.out.println(this.navigation.getNavigation());
    }

}
